package com.trepadora.mapper;

import java.io.IOException;

import com.fourspaces.couchdb.Document;
import com.fourspaces.couchdb.View;
import com.fourspaces.couchdb.ViewResults;
import com.trepadora.model.User;

/**
 * 
 * @author mkorwel
 * 
 */
public class UserMapper extends CouchDBMainMapper {

	/**
	 * Update user in table
	 * 
	 * @param user
	 * @throws IOException
	 */
	public void editUser(final User user) throws IOException {

		Document userDoc = db.getDocument(user.getId());

		userDoc.put("email", user.getEmail());

		db.saveDocument(userDoc);
	}

	/**
	 * Get user by username
	 * 
	 * @param username
	 * @return
	 */
	public User getUserByLogin(final String username) {

		View vi = new View("/_design/user/_view/getUserByUsername");
		vi.setKeyString(username);

		return new User(db.view(vi).getResults().get(0).getJSONObject("value"));

	}
	
	/**
	 * Get user Id by username
	 * 
	 * @param username
	 * @return
	 */
	public String getUserIdByLogin(final String username) {

		View vi = new View("/_design/user/_view/getUserByUsername");
		vi.setKeyString(username);

		return db.view(vi).getResults().get(0).getJSONObject("value").getString("_id");
	}

	/**
	 * 
	 * Save user in table
	 * 
	 * @param username
	 * @param password
	 * @param email
	 * @throws IOException
	 */
	public void saveUser(final String username, final String password,
			final String email) throws IOException {

		Document userDoc = new Document();
		userDoc.put("type", "user");
		userDoc.put("username", username);
		userDoc.put("password", password);
		userDoc.put("email", email);
		userDoc.put("role", "user");
		userDoc.put("busy", false);
		userDoc.put("active", false);

		db.saveDocument(userDoc);

	}
	
	/**
	 * 
	 * Save temporary user in table
	 * 
	 * @param username
	 * @param password
	 * @param email
	 * @throws IOException
	 */
	public String saveTmpUser(final String ipAddress) throws IOException {

		Document userDoc = new Document();
		userDoc.put("type", "user");
		userDoc.put("username", "temp");
		userDoc.put("password", "secret");
		userDoc.put("email", "-");
		userDoc.put("role", "user");
		userDoc.put("busy", false);
		userDoc.put("active", false);
		userDoc.put("ip", ipAddress);

		db.saveDocument(userDoc);

		return userDoc.getId();
	}
	
	/**
	 * Change active state for selected user
	 * 
	 * @param userId
	 * @param state
	 * @throws IOException
	 */
	public boolean setActiveState(final String userId, final boolean state) throws IOException {
		
		Document userProfile = db.getDocument(userId);
		userProfile.put("active", state);
		
		db.saveDocument(userProfile);
		
		return state;
	}
	
	/**
	 * Change busy state for selected user
	 * 
	 * @param userId
	 * @param state
	 * @throws IOException
	 */
	public boolean setBusyState(final String userId, final boolean state) throws IOException {
		
		Document userProfile = db.getDocument(userId);
		userProfile.put("busy", state);
		
		db.saveDocument(userProfile);
		
		return state;
	}
	
	/**
	 * Select one free user to chat
	 * 
	 * @return UserId
	 */
	public String findFreeUser(final String userId) throws IOException {
		
		String freeUserId = null;
		
		// Get all free users with 'active' = true and 'busy' = false
		ViewResults allFreeUsers = getViewResults("user", "freeUsers");

		//TODO: change solution to find one free user
		for (Document d : allFreeUsers.getResults()) {		
				if (!d.getId().equals(userId))
					freeUserId = d.getId();
		}
		
		return freeUserId;
	}
}
